﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.Jobs;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.U2D;
using Random = System.Random;
using ScFramework.Core;

[CustomEditor(typeof(Triangle))]
public class TriangleEditor : Editor
{
    public int seed = 123132;

    public override void OnInspectorGUI()
    {
        
        if (GUILayout.Button("刷新Mesh"))
        {
            (target as Triangle).RefreshMesh(false);
            EditorUtility.SetDirty(target);
        }
        if((target as Triangle).GetComponent<SpriteShapeController>()){

            var tri = target as Triangle;
            if (tri.AutoRefresh|| GUILayout.Button("刷新SpriteShapeMesh"))
            {

            
            var ctr = tri.GetComponent<SpriteShapeController>();
            RefreshBySpriteShape(tri, ctr.spline);
            }
            /*if ()
            {
                seed = (int)Time.frameCount*(int)UnityEngine.Random.Range(-1e6f,1e6f);
            RefreshBySpriteShape(tri, ctr.spline);
            }*/
            
        }
        base.OnInspectorGUI();

    }
    void addTri(Triangle tri,int l,int r,Random rd,Spline spl)
    {

        Debug.Assert(l != r);
        int cnt = spl.GetPointCount();
    //    Debug.Log(l+" "+ r+" "+ cnt);
        if ((l + 1) %cnt == r)
        {
            return;
        }else if ((l + 2) % cnt== r)
        {
            tri.Tris.Add(
                new TriData
                {
                    Type = tri.CommenType,
                    Points = new List<Vector2>
                    {

                spl.GetPosition(l), spl.GetPosition((l + 1) %cnt),
                spl.GetPosition(r)
                    }
                });
            return;
        }
        else
        {
            if (rd.Next(0, 2) == 0)
            {
                tri.Tris.Add(
    new TriData
    {
        Type = tri.CommenType,
        Points = new List<Vector2>
        {

                spl.GetPosition(l), spl.GetPosition((l + 1) %cnt),
                spl.GetPosition(r)
        }
    });
                addTri(tri, (l + 1) % cnt, r, rd, spl);
            }
            else
            {
                tri.Tris.Add(
new TriData
{
Type = tri.CommenType,
Points = new List<Vector2>
{

                spl.GetPosition(l), spl.GetPosition((r - 1+cnt) %cnt),
                spl.GetPosition(r)
}
});
                addTri(tri, l, (r - 1 + cnt) % cnt, rd, spl);
            }
        }

    }
    void RefreshBySpriteShape(Triangle tri,Spline spl)
    {
        //var rd = new Random(seed);
        //int a = rd.Next(0, spl.GetPointCount());
        //int b =( a-1+spl.GetPointCount())% spl.GetPointCount();
        bool reset = false;
        if (spl.GetPointCount() - 2 != tri.Tris.Count)
        {
            tri.Tris.Clear();
            reset = true;
        }

        //Debug.Log(a + " " + b);
        int cnt = spl.GetPointCount();
        int[] lst = { 0, 1, cnt-1 };
        for(int i = 0; i < cnt - 2; i++)
        {
            if (reset)
            {

            tri.Tris.Add(new TriData
            {
                Type = tri.CommenType,
                Points = new List<Vector2>
                {
                    spl.GetPosition(lst[0]),
                    spl.GetPosition(lst[1]),
                    spl.GetPosition(lst[2])
                }
            }) ;
            }
            else
            {

                tri.Tris[i].
                    Points = new List<Vector2>
                    {
                        spl.GetPosition(lst[0]),
                        spl.GetPosition(lst[1]),
                        spl.GetPosition(lst[2])
                    };
            }
            if (tri.Tris[i].IsAntiClock() == false)
            {
                tri.Tris[i].Normalize();
            }
            lst[i%3]=(lst[(i+1)%3]+(i%2==0?1:-1)+cnt)% cnt;
        }
    }
    void SetCenter()
    {
        var lists = (target as Triangle).Tris;
        Vector2 vec = Vector2.zero;
        foreach(var x in lists)
        {
            for(int i = 0; i < 3; i++)
            {
                vec += x.Points[i];
            }
        }
        vec /= lists.Count * 3;
        foreach(var  x in lists)
        {
            for(int i = 0; i < 3; i++)
            {
                x.Points[i] -= vec;
            }
        }
    }
    void CreateLineMesh(float length,float height,int count,float randL,float randH)
    {
        List<TriData> datas = new List<TriData>();
    

        for (int i = 1; i < count; i++)
        {
       //     datas.Add()
        }

    }

}